<?php

namespace app\services;

use plugin\admin\app\model\HelpCenter;

class HelpCenterService
{
    public static function getHelpCenterList(int $page = 1, int $pageSize = 10, ?string $keyword = null): array
    {
        $query = HelpCenter::query();

        // 如果有关键词，添加模糊查询条件
        if (!empty($keyword)) {
            $query->where('title', 'like', '%' . $keyword . '%');
        }

        // 获取总记录数
        $total = $query->count();

        // 分页查询
        $items = $query->offset(($page - 1) * $pageSize)
            ->limit($pageSize)
            ->get()
            ->toArray();

        foreach ($items as $key=>$item) {
            $items[$key]['title'] = get_current_language() == 'en' ? $item['en_title'] : $item['title'];

            $item['content'] = replace_content($item['content']);
            $item['en_content'] = replace_content($item['en_content']);
            $items[$key]['content'] = get_current_language() == 'en' ? $item['en_content'] : $item['content'];
        }

        return [
            'total' => $total,
            'items' => $items,
        ];
    }
}